package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.CooperationIntention;
import com.innovations.inn.service.ICooperationIntentionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 合作意向Controller
 *
 * @author dp
 * @date 2024-08-09
 */
@Api(tags = "合作意向")
@RestController
@RequestMapping("/inn/intention")
public class CooperationIntentionController extends BaseController {
    @Autowired
    private ICooperationIntentionService cooperationIntentionService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询合作意向列表
     */
    @ApiOperation("查询合作意向列表")
    @PreAuthorize("@ss.hasPermi('inn:intention:list')")
    @GetMapping("/list")
    public TableDataInfo list(CooperationIntention cooperationIntention) {
        startPage();
        List<CooperationIntention> list = cooperationIntentionService.selectCooperationIntentionList(cooperationIntention);
        return getDataTable(list);
    }

    /**
     * 导出合作意向列表
     */
    @PreAuthorize("@ss.hasPermi('inn:intention:export')")
    @Log(title = "合作意向", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CooperationIntention cooperationIntention) {
        List<CooperationIntention> list = cooperationIntentionService.selectCooperationIntentionList(cooperationIntention);
        ExcelUtil<CooperationIntention> util = new ExcelUtil<CooperationIntention>(CooperationIntention.class);
        util.exportExcel(response, list, "合作意向数据");
    }

    /**
     * 获取合作意向详细信息
     */
    @ApiOperation("获取合作意向详细信息")
    @PreAuthorize("@ss.hasPermi('inn:intention:query')")
    @GetMapping(value = "/{cooperationIntentionId}")
    public AjaxResult getInfo(@PathVariable("cooperationIntentionId") Long cooperationIntentionId) {
        return success(cooperationIntentionService.selectCooperationIntentionByCooperationIntentionId(cooperationIntentionId));
    }

    /**
     * 新增合作意向
     */
    @ApiOperation("新增合作意向")
    @PreAuthorize("@ss.hasPermi('inn:intention:add')")
    @Log(title = "合作意向", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody CooperationIntention cooperationIntention) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        cooperationIntentionService.completeData(cooperationIntention, userId, 1);
        return toAjax(cooperationIntentionService.insertCooperationIntention(cooperationIntention));
    }

    /**
     * 修改合作意向
     */
    @ApiOperation("修改合作意向")
    @PreAuthorize("@ss.hasPermi('inn:intention:edit')")
    @Log(title = "合作意向", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody CooperationIntention cooperationIntention) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        cooperationIntentionService.completeData(cooperationIntention, userId, 2);
        return toAjax(cooperationIntentionService.updateCooperationIntention(cooperationIntention));
    }

    /**
     * 删除合作意向
     */
    @ApiOperation("删除合作意向")
    @PreAuthorize("@ss.hasPermi('inn:intention:remove')")
    @Log(title = "合作意向", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cooperationIntentionIds}")
    public AjaxResult remove(@PathVariable Long[] cooperationIntentionIds) {
        return toAjax(cooperationIntentionService.deleteCooperationIntentionByCooperationIntentionIds(cooperationIntentionIds));
    }
}
